package es.uned.hoomgle.core.services.impl;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import es.uned.hoomgle.hibernate.beans.interfaces.UserI;
import es.uned.hoomgle.hibernate.daos.interfaces.UserDaoI;

@Service("userDetailsService") 
/**
 * 
 * Obtiene los detalles del usuario autenticado
 *
 */
public class UserDetailService implements UserDetailsService{

	@Resource
	private UserDaoI userDao;
	
	@Resource
	private Assembler assembler;

	
	 @Transactional(readOnly = true)
	 /**
	  * Obtiene los detalles del usuario autenticado
	  */
	  public UserDetails loadUserByUsername(String username)
	      throws UsernameNotFoundException, DataAccessException {
	    UserI userEntity = userDao.findByUserName(username);
	    if (userEntity == null)
	      throw new UsernameNotFoundException("user not found");

	    return assembler.buildUserFromUserEntity(userEntity);
	  }

	
}
